# -*-coding:utf-8-*-

'''
  先计算3月份春羔的各种花费
  按价格不变来走，可以看出总体的趋势
'''

import pandas.io.sql as sql
import pandas as pd
import numpy as np
from pandas import DataFrame, Series
from sqlalchemy import create_engine
import random


# 种羊成本，按月来进行计算，num是羔羊的数量，这里用羔羊数量代替种羊的数量
# 按种羊300只来考虑的话，5年一个轮回

# 雇佣牧工费用，gongzi为每个月工资，按12个月计算
def mugong(gongzi):
    gongzi_month = np.array([0] * 2 + [gongzi] * 10)  # 按12个月计算
    # gongzi_year = gongzi_month.sum() #一年的雇佣成本全都算进去
    gongzi_year = gongzi_month.cumsum()  # 一年累积雇佣成本，按月进行累积，之前牧工成本是直接按年来计算，这样是不合理的

    return gongzi_month, gongzi_year


''' 
# 函数调用调试，检验各值是否正确
gongzi_month, gongzi_year = mugong(4500)	
'''


# 医药水电费平均10元/只，为羊的数量
def yiyao_shuidian(num):
    money = 10  # 医药水电费费10元/只
    yiyao_money = num * 2 * money  # 总数按照卖出羔羊数量的2倍进行计算

    # 医药费按照12个月分摊
    yiyao_money_month = np.array([float(yiyao_money) / 12] * 12)

    return yiyao_money, yiyao_money_month


'''
# 函数调用调试，检验各值是否正确
yiyao_money, yiyao_money_month = yiyao_shuidian(300)
'''


# 固定资产折旧
def zhejiu(jichu_money):
    zhejiu_money = float(jichu_money) / 13  # 固定资产按13年折旧，听说一个牧户建75000元/150平米

    # 固定资产折旧按照12个月平摊
    zhejiu_money_month = np.array([zhejiu_money / 12] * 12)

    return zhejiu_money, zhejiu_money_month


'''
# 函数调用调试，检验各值是否正确
zhejiu_money, zhejiu_money_month = zhejiu(75000)
'''


def zhongyang_busi(num, gancao_price, jingliao_price):
    # 一个羊单位每天的补饲量
    busi_gancao = [0.5] * 4 + [0.4] + [0] * 5 + [0.5] * 2  # 按kg来计算，5月份开始草场返青
    busi_jingliao = [0.3] * 4 + [0.2] + [0] * 4 + [0.2] + [0.3] * 2  # 按kg来进行计算

    # 一个羊单位每个月的补饲量，干草和饲料分开
    busiliang_gancao = np.array(busi_gancao) * 30 * num
    busiliang_jingliao = np.array(busi_jingliao) * 30 * num

    # 一个羊单位每个月的补饲成本，干草和饲料分开
    busi_money_gancao = busiliang_gancao * gancao_price
    busi_money_jingliao = busiliang_jingliao * jingliao_price

    # 一个羊单位每个月的补饲成本，干草和饲料一起
    busi_money_month = busi_money_gancao + busi_money_jingliao
    # print(busi_money_month)
    # 一个羊单位每年的补饲成本
    busi_money_year = busi_money_month.sum()
    # print(busi_money_year)
    return busiliang_gancao, busiliang_jingliao, busi_money_gancao, busi_money_jingliao, busi_money_month, busi_money_year


'''
# 函数调用调试，检验各值是否正确
busiliang_gancao,busiliang_jingliao,busi_money_gancao,busi_money_jingliao,busi_money_month,busi_money_year = zhongyang_busi(
				300, 1.3, 4.5)
'''


# 羔羊的补饲成本	
def lamb_busi(num, gancao_price, jingliao_price):
    # 一个羊单位每天的补饲量
    busi_gancao = [0] * 2 + [0.05] + [0.15] + [0.25] + [0] * 5 + [0.5] * 2  # 按kg来计算
    busi_jingliao = [0] * 2 + [0.05] + [0.1] + [0.15] + [0] * 4 + [0.2] + [0.3] * 2  # 按kg来进行计算

    # 一个羊单位每个月的补饲量，干草和饲料分开
    busiliang_gancao = np.array(busi_gancao) * 30 * num
    busiliang_jingliao = np.array(busi_jingliao) * 30 * num

    # 一个羊单位每个月的补饲成本，干草和饲料分开
    busi_money_gancao = busiliang_gancao * gancao_price
    busi_money_jingliao = busiliang_jingliao * jingliao_price

    # 一个羊单位每个月的补饲成本，干草和饲料一起
    busi_money_month = busi_money_gancao + busi_money_jingliao
    # print(busi_money_month)
    # 一个羊单位每年的补饲成本按月累积结果
    busi_money_year = busi_money_month.sum()
    # print(busi_money_year)
    return busiliang_gancao, busiliang_jingliao, busi_money_gancao, busi_money_jingliao, busi_money_month, busi_money_year


'''
# 函数调用调试，检验各值是否正确
busiliang_gancao,busiliang_jingliao,busi_money_gancao,busi_money_jingliao,busi_money_month,busi_money_year = lamb_busi(
				300, 1.3, 4.5)
'''


# 总的补饲情况
def busi(num, gancao_price, jingliao_price):
    # 羔羊和种羊补饲成本的函数调用
    busiliang_gancao_lamb, busiliang_jingliao_lamb, busi_money_gancao_lamb, busi_money_jingliao_lamb, busi_money_lamb, busi_money_year_lamb = lamb_busi(
        num, gancao_price, jingliao_price)

    busiliang_gancao_zhong, busiliang_jingliao_zhong, busi_money_gancao_zhong, busi_money_jingliao_zhong, busi_money_month_zhong, busi_money_year_zhong = zhongyang_busi(
        num, gancao_price, jingliao_price)

    # 干草和精料的补饲量
    busiliang_gancao = busiliang_gancao_lamb + busiliang_gancao_zhong
    busiliang_jingliao = busiliang_jingliao_lamb + busiliang_jingliao_zhong

    # 干草和精料的补饲成本
    busi_money_gancao = busi_money_gancao_lamb + busi_money_gancao_zhong
    busi_money_jingliao = busi_money_jingliao_lamb + busi_money_jingliao_zhong

    # 总的补饲成本
    busi_money_total = busi_money_gancao + busi_money_jingliao
    # print(busi_money_total)
    # 补饲量和补饲成本的曲线总共5条都有了，并且都在这
    return busiliang_gancao, busiliang_jingliao, busi_money_gancao, busi_money_jingliao, busi_money_total, busi_money_year_zhong, busi_money_lamb


'''
# 函数调用调试，检验各值是否正确
busiliang_gancao,busiliang_jingliao,busi_money_gancao,busi_money_jingliao,busi_money_total,busi_money_year_zhong,busi_money_lamb = busi(
			300, 1.3, 4.5)
'''


# 牧工+水电费+固定资产折旧每个月的分摊费，并且计算养殖总成本和累积成本(包括补饲成本)，
def total_chengben(num, gancao_price, jingliao_price, gongzi, jichu_money):
    # 每个月补饲成本在busi_money_total中
    busiliang_gancao, busiliang_jingliao, busi_money_gancao, busi_money_jingliao, busi_money_total, busi_money_year_zhong, busi_money_lamb = busi(
        num, gancao_price, jingliao_price)

    # 每个月的牧工成本gongzi_month
    gongzi_month, gongzi_year = mugong(gongzi)

    # 每个月的医药水电费yiyao_money_month
    yiyao_money, yiyao_money_month = yiyao_shuidian(num)

    # 每个月的固定资产折旧zhejiu_money_month
    zhejiu_money, zhejiu_money_month = zhejiu(jichu_money)

    # 每个月的养殖总成本，分为雇牧工和不雇牧工(2条曲线有了)
    total_month = busi_money_total + gongzi_month + yiyao_money_month + zhejiu_money_month
    total_month_not = busi_money_total + yiyao_money_month + zhejiu_money_month

    # 每个月的累积成本，分为雇牧工和不雇牧工(2条曲线有了)
    total_month_cumsum = total_month.cumsum()
    total_month_cumsum_not = total_month_not.cumsum()
    # print(total_month_cumsum)
    # print(total_month_cumsum_not)
    # 羔羊不管在什么时候卖，种羊饲料，基础种羊培育费，牧工，水电费，固定资产成本都得按年来计算
    chengben_year = busi_money_year_zhong + busi_money_year_zhong / 5 + gongzi_year + yiyao_money + zhejiu_money
    # print(chengben_year)
    return total_month, total_month_not, total_month_cumsum, total_month_cumsum_not, chengben_year, busi_money_lamb


'''
# 函数调用调试，检验各值是否正确
total_month,total_month_not,total_month_cumsum,total_month_cumsum_not,chengben_year,busi_money_lamb = total_chengben(300,1.3,4.5,4500,75000)
'''


# 计算每只羊的净利润
def profits(num, price, gancao_price, jingliao_price, gongzi, jichu_money):
    # 无政府补贴的情况
    weight_avg = np.array([0, 0, 3.8, 9.9, 16.4, 22.9, 29.4, 35.1, 36.6, 37.2, 35.1, 34])
    zhongyang_weight_avg = np.array([40, 38, 34.2, 36.34, 37.47, 40, 42.33, 43.30, 45.07, 48, 45, 43])
    price_month_not = np.array([price] * 12)

    # 一个羊群不同月份的毛利润，成活率按照98%算，另外羊群按照5年一轮换，基础种羊会更重
    # mao_lirun_not = weight_avg * price_month_not * (num - int(num/5)) * 0.98 + zhongyang_weight_avg * price_month_not * (int(num/5))
    mao_lirun_not = weight_avg * price_month_not * num * 0.98

    # 计算一个羊单位净利润,分为有补贴和无补贴，下面是无补贴的情况，这里牧工成本是按月走，是一个累积结果
    total_month, total_month_not, total_month_cumsum, total_month_cumsum_not, chengben_year, busi_money_lamb = total_chengben(
        num, gancao_price, jingliao_price, gongzi, jichu_money)

    profits_sigle_not = (mao_lirun_not - chengben_year - busi_money_lamb.cumsum()) / num

    # 有政府补贴的情况，其他不变就是价格在6月份增加了2元/kg
    price_month = price_month_not + np.array([0] * 5 + [2] + [0] * 6)
    # mao_lirun = weight_avg * price_month * (num - int(num/5)) * 0.98 + zhongyang_weight_avg * price_month * (int(num/5))
    mao_lirun = weight_avg * price_month * num * 0.98
    profits_sigle = (mao_lirun - chengben_year - busi_money_lamb.cumsum()) / num

    return profits_sigle_not, profits_sigle


'''
# 函数调用调试，检验各值是否正确
profits_sigle_not, profits_sigle = profits(300, 24, 1.3, 4.5, 4500, 75000)
'''


def input():
    num = int(raw_input("请输入出售羊只的数量："))
    price = float(raw_input("请输入羊肉的收购价，按毛重来衡量，多少钱一公斤："))
    gancao_price = float(raw_input("请输入干草的价格，多少钱一公斤："))
    jingliao_price = float(raw_input("请输入精料的价格，多少钱一公斤："))
    gongzi = float(raw_input("请输入所雇牧工的价格，多少钱一个月："))
    jichu_money = float(raw_input("请输入修建羊棚圈所花费用："))

    return num, price, gancao_price, jingliao_price, gongzi, jichu_money


# 最后写上一个主函数来作为启动函数怎么样，哈哈
def main(num=300, price=24, gancao_price=1.3, jingliao_price=4.5, gongzi=4500, jichu_money=75000):
    # 为了调试方便，先把下面一行直接注释掉
    # num, price, gancao_price, jingliao_price, gongzi, jichu_money = input()
    # 首先来看补饲量和补饲成本等5条曲线
    busiliang_gancao, busiliang_jingliao, busi_money_gancao, busi_money_jingliao, busi_money_total, busi_money_year_zhong, busi_money_lamb = busi(
        num, gancao_price, jingliao_price)

    # 再来看养殖总成本等4条曲线
    total_month, total_month_not, total_month_cumsum, total_month_cumsum_not, chengben_year, busi_money_lamb = total_chengben(
        num, gancao_price, jingliao_price, gongzi, jichu_money)

    # 再来看春羔的净利润等2条曲线
    profits_sigle_not, profits_sigle = profits(num, price, gancao_price, jingliao_price, gongzi, jichu_money)
    print "没有政府补贴的净利润：\n", profits_sigle_not
    print "\n有政府补贴的净利润：\n", profits_sigle

    # 最后来构造一个表将所有的记录都插入到表中，最后将表插入到数据库中
    names = ['shijian', 'busiliang_gancao', 'busiliang_jingliao', 'busi_money_gancao', 'busi_money_jingliao',
             'busi_money_total',
             'total_month', 'total_month_not', 'total_month_cumsum', 'total_month_cumsum_not', 'profits_sigle_not',
             'profits_sigle']

    data = [np.arange(1, 13), busiliang_gancao, busiliang_jingliao, busi_money_gancao, busi_money_jingliao,
            busi_money_total,
            total_month, total_month_not, total_month_cumsum, total_month_cumsum_not, profits_sigle_not, profits_sigle]

    # 由于所有的记录都被当作行来处理，所以这个时候需要转置一下数据，数据已经全部处理好这个时候需要存储到数据库中
    result = DataFrame(data, index=names, columns=range(1, 13))
    data_result = result.T

    # data_result.to_csv(u'D:\\weight_learning\\春羔修改4_2.csv') # 将所有数据记录插入到表中

    # 将所有记录插入到数据库中，因为to_sql函数支持sqlalchemy引擎，所以使用sqlalchemy中的create_engine创建mysql连接
    # conn = create_engine('mysql+mysqldb://root:root@10.129.22.113:3306/dbsheep?charset=utf8') # 创建连接

    # to_sql函数在pandas.io.sql中,这里 sql 表示的是pandas.io.sql,使用create_engine无需释放数据库连接，winter_lamb是表名
    # sql.to_sql(data_result, 'spring_lamb', con = conn, if_exists = 'replace')

    # 构造字典以便实现json数据格式的上传
    gouzao_dict = {
        'shijian': list(np.arange(1, 13)),
        'busiliang_gancao': list(busiliang_gancao),
        'busiliang_jingliao': list(busiliang_jingliao),
        'busi_money_gancao': list(busi_money_gancao),
        'busi_money_jingliao': list(busi_money_jingliao),
        'busi_money_total': list(busi_money_total),
        'total_month': list(total_month),
        'total_month_not': list(total_month_not),
        'total_month_cumsum': list(total_month_cumsum),
        'total_month_cumsum_not': list(total_month_cumsum_not),
        'profits_sigle_not': list(profits_sigle_not),
        'profits_sigle': list(profits_sigle)
    };
    print type(gouzao_dict)

    return gouzao_dict


if __name__ == '__main__':
    json_data = main()
